package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyBxdaMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxBaoxiaodanParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ProjectParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.model.result.SelectResult;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案管理表 服务实现类
 * </p>
 *
 * @author liuy
 * @since
 */
@Service
public class BtyBxdaService extends ServiceImpl<BtyBxdaMapper, BtyBxda> {

    /**
     * 获取档案管理列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:05 PM
     */

    public Page<Map<String, Object>> list(String condition,String kj,String ghs,String dm) {
        Page page = LayuiPageFactory.defaultPage();

        return this.baseMapper.list(page, condition,kj,ghs,dm);
    }

    public Page<Map<String, Object>> listZc(String condition,String kj,String dm) {
        Page page = LayuiPageFactory.defaultPage();

        return this.baseMapper.listZc(page, condition,kj,dm);
    }
    public List<BtyBxda> dmList(String kj) {
        return this.baseMapper.dmList(kj);
    }

    /**
     * 批量修改报销档案的会计
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatekj(String kj, String dm) {
          baseMapper.updatekj(kj,dm);
          baseMapper.updatekjs(kj,dm);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBxdaKj(String kj, String dm) {
        baseMapper.updatekj(kj,dm);
    }
    /**
     * 设置供货商openid
     */
    public  int setOpenid(String openid,String xm,String yhk){
        return this.baseMapper.setOpenid(openid,xm,yhk);
    }
    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    public LayuiPageInfo getAllVendor(ProjectParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.getAllVendor(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }
    public List<Map<String,Object>> getAllVendorList(String dm) {
        List<Map<String,Object>> list = this.baseMapper.getAllVendorList(dm);
        return list;
    }

    public List<SelectResult> getZxList(String dm) {
        List<SelectResult> list = this.baseMapper.getZxList(dm);
        return list;
    }
    public String getBankCode(String bank){
       return baseMapper.getBankCode(bank);
    }
    public List<BtyBxdaResult> getDmByTel(String tel){
       return baseMapper.getDmByTel(tel);
    }
    public List<BtyBxdaResult> getListByTel(String tel){
        return baseMapper.getListByTel(tel);
    }

    //根据姓名获取报销档案 门店装修
    public BtyBxdaResult getByName(String md, String name){
       return this.baseMapper.getByName(md,name);
    }

    public List<Map<String, String>> getBankCodeAll(){
       return baseMapper.getBankCodeAll();
    }

    /**
     * 获取所有银行卡
     * @return
     */
    public List<String> getYhk() {
        return baseMapper.getYhk();
    }

    /**
     * 填写银行简称
     * @param btyBxda
     * @return
     */
    public int updateBank(BtyBxda btyBxda) {
        return baseMapper.updateBank(btyBxda);
    }
    /**
     * 填写银行简称
     * @param btyBxda
     * @return
     */
    public int add(BtyBxda btyBxda) {
        return baseMapper.insert(btyBxda);
    }

    /**
     * 查询该行号对应银行简称
     * @param bz
     * @return
     */
    public Map<String, String> getBank(String bz) {
        return baseMapper.getBank(bz);
    }

    public List<BtyBxda> isNullList() {
        return baseMapper.isNullList();
    }

    public void updateByYhk(BtyBxda btyBxda) {
        baseMapper.updateByYhk(btyBxda);
    }

    public BtyBxdaResult getByTel(String md, String tel) {
        return this.baseMapper.getByTel(md,tel);
    }

    public BtyBxdaResult getByYhk(String dm, String yhk) {
        return this.baseMapper.getByYhk(dm,yhk);
    }

    public void initBxdaZc(String tel, String xingming) {
        //获取列表
        List<BtyBxdaResult> list = this.baseMapper.queryMdsList(tel);
        for (BtyBxdaResult b:list){
            String ym = b.getYm();
            String dm = b.getDm();
            String kj = getKjByYaoma(ym);
            if(!isHasBxda(tel,dm)){
                BtyBxda bean = new BtyBxda();
                bean.setTel(tel);
                bean.setXm(xingming);
                bean.setKj(kj);
                bean.setDm(dm);
                bean.setYaoma(ym);
                bean.setGhs("门店自采");
               this.baseMapper.insert(bean);
            }
        }
    }

    public boolean isHasBxda(String tel,String dm){
        String id = this.baseMapper.queryIdByTelAndMd(tel,dm);
        if(id!=null && !id.equals("")){
            return true;
        }else{
            return false;
        }
    }

    private Map<String,String> yaomaKjMap = new HashMap<String,String>();
    private String getKjByYaoma(String yaoma){
        if(!yaomaKjMap.containsKey(yaoma)){
            List<Map<String, String>> ymkjMaptmp=this.baseMapper.queryYmkjMap();
            for(Map<String, String> m:ymkjMaptmp){
                String ym = m.get("yaoma");
                String cwry = m.get("cwry");
                yaomaKjMap.put(ym,cwry);
            }
        }
        return yaomaKjMap.get(yaoma);
    }

    /**
     * 根据手机号查询id
     *
     * @author zxd
     * @Date 2022-04-22
     */
    public List<BtyBxda> bxdaList(String tel) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("tel", tel);
        qw.eq("dm", "半天妖");
        List<BtyBxda> btyBxdaList = this.baseMapper.selectList(qw);
        return btyBxdaList;
    }
}
